package com.cloud.common.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 格式工具类
 *
 * @author fengwenjin
 */
public class FormatUtils {
    private static final String MASK_SYMBOL = "*";

    /**
     * 根据给定的格式进行转换
     *
     * @param value 待转换数据
     * @param format 给定的格式，必须包含符号*
     * @return
     */
    public static String maskFormat(String value, String format) {
        if (StringUtils.isEmpty(value) || StringUtils.isEmpty(format) || format.indexOf(MASK_SYMBOL) == -1) {
            return value;
        }
        int headIndex = format.indexOf(MASK_SYMBOL);
        int footIndex = format.lastIndexOf(MASK_SYMBOL) + 1;
        StringBuilder formatValue = new StringBuilder();
        formatValue.append(value, 0, headIndex);
        formatValue.append(format, headIndex, footIndex);
        formatValue.append(value.substring(value.length() - format.substring(footIndex).length()));
        return formatValue.toString();
    }
}
